<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class LinkCat extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\LinkCatModel();
        $this->validate=new \app\admin\validate\LinkCatValidate();
    } 
    //
    public function index(){
        $data=$this->model->order("sort asc,catid asc")->select();  
        View::assign('data', $data);
        return View::fetch();
    }
    //
    public function add(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $this->model->save($data);
            $catid=$this->model->catid;
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$catid,$this->request->param());            
            $this->success($catid);
        }else{
            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=$this->request->param();
            if(!$this->validate->check($data)){
                $this->error($this->validate->getError());
            }
            $this->model->update($data);
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['catid'],$this->request->param());            
            $this->success($catid);
        }else{
            $catid=Request::param('catid',0,"intval");
            $r=$this->model->find($catid);
            View::assign("r",$r);        
            return View::fetch();
        }
    }   
    public function delete(){
        $id=Request::param("catid",0,"intval");
        if(!$id){
             $this->error=('请选择信息！');
        }
        $this->model->destroy($id);
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id,$this->request->param());  
        $this->success("删除成功");
    } 
    //字段排序
    public function updateSort(){
        $catids=Request::param("ids");
        $sort=Request::param("sort");
        foreach($catids as $key=>$catid){
            $data['catid']=(int)$catid;
            $data['sort']=(int)$sort[$key];
            $this->model->update($data);
        }
        $this->success("排序更改成功");
    }       
}
